<?php

/*
	[Discuz!] (C)2001-2009 Comsenz Inc.
	This is NOT a freeware, use is subject to license terms

	$Id$
*/

function sqldebug($sql='') {
	global $sqldebug;

	static $sqlstarttime = 0, $process = 0, $start = 0;

	$mtime = explode(' ', microtime());
	if(empty($sql) || empty($sqlstarttime)) {
		$process = 0;
		$sqlstarttime = $mtime[1] + $mtime[0];
		$start = number_format($sqlstarttime - $GLOBALS['discuz_starttime'], 6);
		return true;
	} else {
		$process = number_format($mtime[1] + $mtime[0] - $sqlstarttime, 6) ;
		$sqlstarttime = 0;
	}

	$sqldebug .= "<center>
		<div style=\"border:1px solid black;padding:1em;margin:2px;text-align:left; width:96%;clear:both;\">
		<div style=\"font-size:12px; float:right; width:250px; margin-bottom:5px; border: 1px solid red; padding: 4px;\"><b>SQL Start: </b>$start<b> Processed:</b> $process</b> $sqlendttime</div><span style=\"font-size:12px\">".sqlshowformat($sql).'</font>';
	if(preg_match("/^select /i", $sql)) {
		$sqldebug .= "<table width=\"100%\" style=\"margin-top: 5px;font-size:12px;display:block;background:#fff\" cellspacing=\"1\" cellpadding=\"0\"><tr style=\"background:#FFF\"><td>id</td><td>table</td><td>type</td><td>possible_keys</td><td>key</td><td>key_len</td><td>ref</td><td>rows</td><td>Extra</td></tr>";

		$explainquery = mysql_query("explain $sql");
		if($explainquery) {
			while( $explain = mysql_fetch_assoc($explainquery) ) {
				$sqldebug .= "<tr style=\"background: #E4E4E4\"><td>&nbsp;{$explain['id']}</td><td>&nbsp;{$explain['table']}</td><td>&nbsp;{$explain['type']}</td><td>&nbsp;{$explain['possible_keys']}</td><td>&nbsp;{$explain['key']}</td><td>&nbsp;{$explain['key_len']}</td><td>&nbsp;{$explain['ref']}</td><td>&nbsp;{$explain['rows']}</td><td>&nbsp;{$explain['Extra']}</td></tr>";
			}
		}
		$sqldebug .= "</table>";
	}
	$sqldebug .="</div><br /></center>\r\n";
}

function debugtools() {
	global $_COOKIE, $_SESSION, $_DCOOKIE, $_DCACHE, $_DSESSION, $_DCACHE, $_DPLUGIN, $sqldebug, $debuginfo, $sqlspenttimes;
	$table1 = "<table cellspacing=0 cellpadding=0 width=100% align=center><tr><td class=altbg2 style=\"font-size:12px;\">";
	$table2 = "</td></tr></table>";
	$sqldebuglist = $table1.$sqldebug.$table2;
	$dcookielist = $table1."<div style=\"border:1px soild black;\">&#36;_DCOOKIE</div><br>".preg_replace(array('/\n/', '/\s+/'), array('<br>', '&nbsp;'), daddslashes(print_r($_DCOOKIE, true), '"'));
	$dcookielist .= "<div style=\"border:1px soild black;\">&#36;_COOKIE</div><br>".preg_replace(array('/\n/', '/\s+/'), array('<br>', '&nbsp;'), daddslashes(print_r($_COOKIE, true), '"')).$table2;
	$includelist = $table1.preg_replace(array('/\n/', '/\s/'), array('<br>', '&nbsp;'), daddslashes(print_r(get_included_files(), true), '"')).$table2;
	$dsessionlist = $table1."<div style=\"border:1px soild black;\">&#36;_DSESSION</div><br>".preg_replace(array('/\n/', '/\s+/'), array('<br>', '&nbsp;'), daddslashes(print_r($_DSESSION, true), '"')).$table2;
	$dsessionlist .= $table1."<div style=\"border:1px soild black;\">&#36;_SESSION</div><br>".preg_replace(array('/\n/', '/\s+/'), array('<br>', '&nbsp;'), daddslashes(print_r($_SESSION, true), '"')).$table2;
	$dcachelist = $table1."<div style=\"border:1px soild black;\">&#36;_DCACHE</div><br>".preg_replace(array('/\n/', '/\s+/'), array('<br>', '&nbsp;'), daddslashes(print_r($_DCACHE, true), '"')).$table2;
	$dpluginlist = $table1."<div style=\"border:1px soild black;\">&#36;_DPLUGIN</div><br>".preg_replace(array('/\n/', '/\s+/'), array('<br>', '&nbsp;'), daddslashes(print_r($_DPLUGIN, true), '"')).$table2;
	$debugwinenv = $table1."<ul><li>PHP版本: ".PHP_VERSION."</li><li>MYSQL版本: ".mysql_get_server_info()."</li></ul>".$table2;

	$out  = "<div id=debug_bar style=\"background:".ALTBG2.";text-align:left;padding: 0 10px\">";
	$out .= "<table cellspacing=0 cellpadding=0 width=100% align=\"center\">";
	$out .= "<tr><td colspan=\"3\">";
	$out .= "<span style=\"cursor:pointer;\" onclick=\"$('debug_bar').style.display = 'none'\">[关闭]</span>";
	$out .= "<span style=\"cursor:pointer;\" onclick=\"debugdisplay('debugwinquery')\"> | [查询 ".$debuginfo['queries']." 次]</span>";
	$out .= "<span style=\"cursor:pointer;\" onclick=\"debugdisplay('debugwininclude')\"> | [引用".count(get_included_files())." 个]</span>";
	$out .= "<span style=\"cursor:pointer;\" onclick=\"debugdisplay('debugwindcookie')\"> | [DCOOKIE]</span>";
	$out .= "<span style=\"cursor:pointer;\" onclick=\"debugdisplay('debugwindsession')\"> | [DSESSION]</span>";
	$out .= "<span style=\"cursor:pointer;\" onclick=\"debugdisplay('debugwinplugin')\"> | [DPLUGIN]</span>";
	$out .= "<span style=\"cursor:pointer;\" onclick=\"debugdisplay('debugwinenv')\"> | [环境]</span>";
	$out .= "</td></tr></table>";

	$out .= "<div id=\"debugwinquery\" style=\"display:none;\">".$sqldebuglist."</div>";
	$out .= "<div id=\"debugwindcookie\" style=\"display:none;\">".$dcookielist."</div>";
	$out .= "<div id=\"debugwindsession\" style=\"display:none;\">".$dsessionlist."</div>";
	$out .= "<div id=\"debugwinplugin\" style=\"display:none;\">".$dpluginlist."</div>";
	$out .= "<div id=\"debugwininclude\" style=\"display:none;\">".$includelist."</div>";
	$out .= "<div id=\"debugwinenv\" style=\"display:none;\">".$debugwinenv."</div>";

	$out .= <<<EOF
<script>
var debugwinarray = new Array(
'debugwinquery', 'debugwininclude', 'debugwindcookie'
, 'debugwindsession', 'debugwincache', 'debugwinplugin'
, 'debugwinuser', 'debugwinpower', 'debugwingroup'
, 'debugwinsettings', 'debugwinenv'
)
function debugdisplay(debugwinid) {
	for(i = 0; i < debugwinarray.length; i++) {
		if(did = document.getElementById(debugwinarray[i])) {
			if(debugwinid == debugwinarray[i]) {
				did.style.display = did.style.display == '' ? 'none' : '';
			}else{
				did.style.display = 'none';
			}
		}
	}
}
</script>
EOF;

	echo "\n\n\n\n\n\n\n\n\n".$out;
}



function sqlshowformat($sql) {

	$sql = preg_replace('/(SELECT|FROM|WHERE|DELETE|UPDATE|SET|INSERT INTO|REPLACE INTO|ORDER BY|GROUP BY) /i', '<b>\\1 </b>', htmlspecialchars($sql));

	return $sql;
}
?>